package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.Login;
import com.example.mall.domain.Permission;
import com.example.mall.dto.LoginDto;
import com.example.mall.dto.PermissionDto;
import com.example.mall.mapper.PermissionMapper;
import com.example.mall.query.LoginQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PermissionQuery;
import com.example.mall.service.IPermissionService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Copy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-04-30
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageDTO<PermissionDto> queryPage(PermissionQuery permissionQuery) {
        if (permissionQuery == null) {
            permissionQuery = new PermissionQuery();
        }
        Page<Permission> page = permissionQuery.toMpPageByDefaultCreateTimeAsc();
        Page<Permission> cartPage = lambdaQuery()
                .like(permissionQuery.getPermissionId() != null, Permission::getPermissionId, permissionQuery.getPermissionId())
                .like(StringUtils.isNotBlank(permissionQuery.getPermissionName()), Permission::getPermissionName, permissionQuery.getPermissionName())
                .like(StringUtils.isNotBlank(permissionQuery.getPermissionDescription()), Permission::getPermissionDescription, permissionQuery.getPermissionDescription())
                .like(StringUtils.isNotBlank(permissionQuery.getPermissionState()), Permission::getPermissionState, permissionQuery.getPermissionState())
                .orderByDesc(Permission::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PermissionDto.class);
    }

    @Override
    public Permission queryPermissionById(Long permissionId) {
        return permissionMapper.selectById(permissionId);
    }

    @Override
    public List<Permission> queryPermissionList() {
        return this.list();
    }

    @Override
    public void insert(PermissionDto permissionDto) {
        Permission permission = new Permission();
        CopyUtils.copyProperties(permissionDto, permission);
        permission.setPermissionId(CodeGenerateUtils.generateUniqueId());
        permission.setCreateDate(new Date());
        permission.setUpdateDate(new Date());
        permissionMapper.insert(permission);
    }

    @Override
    public void update(PermissionDto permissionDto) {
        AssertUtil.isNotNull(permissionDto.getPermissionId(), "权限id不能为空");
        Permission permission = permissionMapper.selectById(permissionDto.getPermissionId());
        AssertUtil.isNotNull(permission, "权限不存在");
        CopyUtils.copyProperties(permissionDto, permission);
        permission.setUpdateDate(new Date());
        permissionMapper.updateById(permission);
    }

    @Override
    public void delete(Long permissionId) {
        AssertUtil.isNotNull(permissionId, "权限id不能为空");
        Permission permission = permissionMapper.selectById(permissionId);
        AssertUtil.isNotNull(permission, "权限不存在");
        permission.setPermissionState(String.valueOf(BaseConstants.LOSE_STATUS));
        permission.setUpdateDate(new Date());
        permissionMapper.updateById(permission);
    }

    @Override
    public void deleteBatch(List<Long> permissionIds) {
        AssertUtil.isNotNull(permissionIds, "权限id不能为空");
        List<Permission> permissions = permissionMapper.selectBatchIds(permissionIds);
        AssertUtil.isNotNull(permissions, "权限不存在");
        permissions.stream().forEach(item->{
            item.setPermissionState(String.valueOf(BaseConstants.LOSE_STATUS));
            item.setUpdateDate(new Date());
            permissionMapper.updateById(item);
        });
    }
}
